/*
 Copyright (c) 2010, Igor L. B.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the iglaweb nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Igor L. B. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace NotifyClient
{
	
	public class NotifyIcon
	{
		//Declare click event
		public event System.EventHandler Click;

		private WindowSink windowSink;
		private int uID = 5000;

		//Constructor
		public NotifyIcon()
		{
			//Create instance of the MessageWindow subclass
			windowSink = new WindowSink(this);
			windowSink.uID = uID;
		}

		//Destructor
		~NotifyIcon()
		{
			Remove();			
		}


		public void Add(IntPtr hIcon)
		{
			TrayMessage(windowSink.Hwnd, NIM_ADD, (uint)uID, hIcon);
		}

		public void Remove()
		{
		
			TrayMessage(windowSink.Hwnd, NIM_DELETE, (uint)uID, IntPtr.Zero);
		}

		public void Modify(IntPtr hIcon)
		{
			
			TrayMessage(windowSink.Hwnd, NIM_MODIFY, (uint)uID, hIcon);

		}



		private void TrayMessage(IntPtr hwnd, int dwMessage, uint uID, IntPtr hIcon)
		{
			NOTIFYICONDATA notdata = new NOTIFYICONDATA();

			notdata.cbSize = 152;
			notdata.hIcon =  hIcon;
			notdata.hWnd = hwnd;
			notdata.uCallbackMessage = WM_NOTIFY_TRAY;
			notdata.uFlags = NIF_MESSAGE | NIF_ICON;
			notdata.uID = uID;

			int ret = Shell_NotifyIcon(dwMessage, ref notdata);
		} 

		#region API Declarations

		internal const int WM_LBUTTONDOWN = 0x0201;
		//User defined message
		internal const int WM_NOTIFY_TRAY = 0x0400 + 2001;

		internal const int NIM_ADD       =  0x00000000;
		internal const int NIM_MODIFY    =  0x00000001;
		internal const int NIM_DELETE    =  0x00000002;

		const int NIF_MESSAGE   =  0x00000001;
		const int NIF_ICON      =  0x00000002;


		internal struct NOTIFYICONDATA 
		{ 
			internal int cbSize;
			internal IntPtr hWnd; 
			internal uint uID; 
			internal uint uFlags; 
			internal uint uCallbackMessage; 
			internal IntPtr hIcon; 
			//internal char[] szTip = new char[64]; 
			//internal IntPtr szTip;
		}

		[DllImport("coredll.dll")]
		internal static extern int Shell_NotifyIcon(
			int dwMessage,ref NOTIFYICONDATA pnid);
		
		[DllImport("coredll.dll")]
		internal static extern int SetForegroundWindow(IntPtr hWnd);
		
		[DllImport("coredll.dll")]
		internal static extern int ShowWindow( 
			IntPtr hWnd, 
			int nCmdShow); 
		
		[DllImport("coredll.dll")]
		internal static extern IntPtr GetFocus();

		#endregion


		#region WindowSink

		internal class WindowSink : Microsoft.WindowsCE.Forms.MessageWindow
		{
			//Private members
			private int m_uID = 0;
			private NotifyIcon notifyIcon;

			//Constructor
			public WindowSink(NotifyIcon notIcon)
			{
				notifyIcon = notIcon;	
			}
			
			public int uID
			{
				set
				{
					m_uID = value;

				}	
			}

			protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
			{
				 
				if (msg.Msg == WM_NOTIFY_TRAY)
				{
					if((int)msg.LParam == WM_LBUTTONDOWN)
					{
						if ((int)msg.WParam == m_uID)
						{
							//If somebody hooked, raise the event
							if (notifyIcon.Click != null)
								notifyIcon.Click(notifyIcon, null);
		
						}
					}	
				}
								
			}
		}
		#endregion

	}
}
